package com.clippick.dbbusiness;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
// import android.util.Log;

import com.clippick.common.GlobalValue;
import com.clippick.database.ClipData;
import com.clippick.database.FeedOrderDBAdapter;
import com.clippick.database.HistoryDBAdapter;

public class DBBusiness {
	private static HistoryDBAdapter dbHistory;
	private static FeedOrderDBAdapter dbFeedOrder;

	public static void startDBs(Context c) {
		dbHistory = new HistoryDBAdapter(c, GlobalValue.getDbTableName(c));
		dbFeedOrder = new FeedOrderDBAdapter(c, GlobalValue.getDbTableName(c));

		openDbs();

		if (checkTableExists(GlobalValue.getDbTableName(c)).equals("")) {
			dbHistory.createNewTable(GlobalValue.getDbTableName(c));
		}
		
		if (checkImageOrderTableExists(GlobalValue.getImageOrderDbTableName(c)).equals("")) {
			dbFeedOrder.createNewImageOrderTable(GlobalValue.getImageOrderDbTableName(c));
		}
	}
	
	public static String checkImageOrderTableExists(String tableName) {
		return dbFeedOrder.checkTableExists(tableName);
	}

	public static String checkTableExists(String tableName) {
		return dbHistory.checkTableExists(tableName);
	}

	/**
	 * public static void openHistoryDb() { dbHistory.open(); }
	 * 
	 * public static void closeHistoryDb() { dbHistory.close(); }
	 * 
	 * public static void openFeedOrderDb() { dbFeedOrder.open(); }
	 * 
	 * public static void closeFeedOrderDb() { dbFeedOrder.close(); }
	 */

	public static void openDbs() {
		try {
			dbHistory.open();
			dbFeedOrder.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void checkDbOpen() throws Exception {
		if (!dbHistory.isOpenDBfile()){
			dbHistory.open();
		}
		
		if (!dbFeedOrder.isOpenDBfile()){
			dbFeedOrder.open();
		}
	}
	
	public static void closeDbs() {
		try {
			dbHistory.close();
			dbFeedOrder.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void clearDbs() {
		dbHistory = null;
		dbFeedOrder = null;
	}

	@SuppressWarnings("rawtypes")
	public static List<Map> fetchStarClipFromDb() {
		/*
		 * dbHistory.open(); dbFeedOrder.open();
		 */
		List<Map> retList = new ArrayList<Map>();
		try {
			checkDbOpen();
			retList = ClipData.fetchStarClipFromDb(dbHistory, dbFeedOrder);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			/*
			 * dbHistory.close(); dbFeedOrder.close();
			 */
		}
		return retList;
	}

	@SuppressWarnings("rawtypes")
	public static List<Map> fetchClipFromDb() {
		/*
		 * dbHistory.open(); dbFeedOrder.open();
		 */
		List<Map> retList = new ArrayList<Map>();
		try {
			checkDbOpen();
			retList = ClipData.fetchClipFromDb(dbHistory, dbFeedOrder);
		} catch (Exception e) {
			e.printStackTrace();

		} finally {
			/*
			 * dbHistory.close(); dbFeedOrder.close();
			 */
		}
		return retList;
	}

	/**
	 * warning: this function doesn't open/close database. you have to manage
	 * the database yourself using <b>open()</b> and <b>close()</b> function
	 * corresponding
	 * 
	 * @param index
	 * @return
	 */
	public static Map<Object, Object> compareFeed() {
		// dbHistory.open();
		Map<Object, Object> retMap = new HashMap<Object, Object>();
		try {
			checkDbOpen();
			retMap = ClipData.compareFeed(dbHistory);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// dbHistory.close();
		}
		return retMap;
	}

	/**
	 * warning: this function doesn't open/close database. you have to manage
	 * the database yourself using <b>open()</b> and <b>close()</b> function
	 * corresponding
	 * 
	 * @param index
	 * @return
	 */
	public static long insertFeedFromFeedOrder(long idx, String order,
			String textOrPath, String imageUrl) {
		// dbFeedOrder.open();
		try {
			checkDbOpen();
			return dbFeedOrder.insertFeed(idx, order, textOrPath, imageUrl);
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		} finally {
			// dbFeedOrder.close();
		}
	}

	/**
	 * warning: this function returns a Cursor object and doesn't open/close
	 * database. you have to manage the database yourself using <b>open()</b>
	 * and <b>close()</b> function corresponding
	 * 
	 * @param idx
	 * @return
	 * @throws SQLException
	 */
	public static Cursor fetchFeedByIndex(long idx) {
		try {
			checkDbOpen();
			return dbFeedOrder.fetchFeedByIndex(idx);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * warning: this function doesn't open/close database. you have to manage
	 * the database yourself using <b>open()</b> and <b>close()</b> function
	 * corresponding
	 * 
	 * @param index
	 * @return
	 */
	public static boolean deleteFeedFromFeedOrder(long index) {
		try {
			checkDbOpen();
			return dbFeedOrder.deleteFeedByIndex(index);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * warning: this function doesn't open/close database. you have to manage
	 * the database yourself using <b>open()</b> and <b>close()</b> function
	 * corresponding
	 * 
	 * @param index
	 * @return
	 */
	public static long insertFeedFromHistory(Map<String, String> feed) {
		// dbHistory.open();
		long retVal = -1;
		try {
			checkDbOpen();
			retVal = dbHistory.insertFeed(feed);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// dbHistory.close();
		}
		return retVal;
	}

	/**
	 * warning: this function doesn't open/close database. you have to manage
	 * the database yourself using <b>open()</b> and <b>close()</b> function
	 * corresponding
	 * 
	 * @param feed
	 * @return
	 */
	public static boolean updateFeedFromHistoryDb(Map<String, String> feed) {
		try {
			checkDbOpen();
			return dbHistory.updateFeed(feed);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * warning: this function doesn't open/close database. you have to manage
	 * the database yourself using <b>open()</b> and <b>close()</b> function
	 * corresponding
	 * 
	 * @param feed
	 * @return
	 */
	public static long insertFeedIntoTempTable(Map<String, String> feed) {
		try {
			checkDbOpen();
			return dbHistory.insertFeedIntoTempTable(feed);
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	public static List<String> getAllText() {
		// openHistoryDb();
		List<String> retList = new ArrayList<String>();
		try {
			checkDbOpen();
			retList = dbHistory.getAllText();
			return retList;
		} catch (Exception e) {
			e.printStackTrace();
			return retList;
		} finally {
			// closeHistoryDb();
		}
	}

	/**
	 * warning: this function returns a Cursor object and doesn't open/close
	 * database. you have to manage the database yourself using <b>open()</b>
	 * and <b>close()</b> function corresponding
	 * 
	 * @param index
	 * @return Cursor
	 * @throws SQLException
	 */
	public static Cursor getFeedByIndex(long index) {
		try {
			checkDbOpen();
			return dbHistory.getFeedByIndex(index);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * warning: this function doesn't open/close database. you have to manage
	 * the database yourself using <b>open()</b> and <b>close()</b> function
	 * corresponding
	 * 
	 * @param index
	 * @return
	 */
	public static boolean deleteFeedFromHistoryDb(long index) {
		try {
			checkDbOpen();
			return dbHistory.deleteFeed(index);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
}
